package com.twb.system.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;
import com.twb.system.entity.twbDataDic.TwbDataDic;
import com.twb.system.entity.twbDataDic.dto.GetDataDicByTypeCodeDto;
import com.twb.system.entity.twbDataDic.dto.GetTwbDataDicByIdDto;
import com.twb.system.entity.twbDataDic.dto.ListTwbDataTypeDto;
import com.twb.system.entity.twbDataDic.dto.SearchTwbDataDicDto;
import com.twb.system.entity.twbDataDic.dto.TwbDataDicDto;
import com.twb.system.entity.twbDataDic.vo.SaveTwbDataDicVo;
import com.twb.system.entity.twbDataDic.vo.SearchTwbDataDicVo;
import com.twb.system.entity.twbDataDic.vo.UpdateTwbDataDicByIdVo;
import com.twb.system.service.ITwbDataDicService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Title  TwbDataDicController
 * @Description 数据字典
 * 
 * @author DSY
 * @date 2020-11-05 12:54:33
 */
@RestController
@RequestMapping("/system/twbDataDic")
@Api(tags = "数据字典", value = "TwbDataDicController")
public class TwbDataDicController extends BaseController {
	@Autowired
	ITwbDataDicService twbDataDicService;

	/**
	 * @Title  searchTwbDataDic
	 * @Description 分页查询
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2020-07-27 15:33:39
	 */

	@GetMapping("/search")
	@ApiOperation(value = "分页查询")
	public Result<TwbPageInfo<SearchTwbDataDicDto>> searchTwbDataDic(SearchTwbDataDicVo vo) {
		TwbPageInfo<SearchTwbDataDicDto> result = twbDataDicService.searchTwbDataDic(vo);
		return Results.successWithData(result);
	}

	/**
	 * @Title  getTwbDataDicById
	 * @Description 根据ID获取详情
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2020-07-27 15:33:39
	 */

	@GetMapping("/getById")
	@ApiOperation(value = "根据ID获取详情")
	public Result<GetTwbDataDicByIdDto> getTwbDataDicById(@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String id) {
		TwbDataDic twbDataDic = twbDataDicService.getById(id);
		if (twbDataDic == null) {
			return Results.failure("数据不存在");
		}
		GetTwbDataDicByIdDto dto = new GetTwbDataDicByIdDto();
		BeanUtils.copyNotNullProperties(twbDataDic, dto);
		return Results.successWithData(dto);
	}

	/**
	 * @Title  saveTwbDataDic
	 * @Description 保存信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2020-07-27 15:33:39
	 */

	@PostMapping("/save")
	@ApiOperation(value = "保存信息")
	public Result<String> saveTwbDataDic(@Valid @RequestBody SaveTwbDataDicVo vo) {
		int result = twbDataDicService.saveTwbDataDic(vo);
		if (result > 0) {
			return Results.success("保存成功");
		} else {
			return Results.failure("保存失败");
		}
	}

	/**
	 * @Title  updateTwbDataDicById
	 * @Description 更新信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2020-07-27 15:33:39
	 */

	@PutMapping("/updateById")
	@ApiOperation(value = "更新信息")
	public Result<String> updateTwbDataDicById(@Valid @RequestBody UpdateTwbDataDicByIdVo vo) {
		int result = twbDataDicService.updateTwbDataDicById(vo);
		if (result > 0) {
			return Results.success("更新成功");
		} else {
			return Results.failure("更新失败");
		}
	}

	/**
	 * @Title  deleteTwbDataDicById
	 * @Description 删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2020-07-27 15:33:39
	 */

	@PostMapping("/deleteById")
	@ApiOperation(value = "删除信息")
	public Result<String> deleteTwbDataDicById(@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String id) {
		int result = twbDataDicService.deleteTwbDataDicById(id);
		if (result > 0) {
			return Results.success("删除成功");
		} else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title  deleteBatchTwbDataDicByIds
	 * @Description 批量删除信息
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2020-07-27 15:33:39
	 */

	@PostMapping("/batchDeleteByIds")
	@ApiOperation(value = "批量删除信息")
	public Result<String> deleteBatchTwbDataDicByIds(@NotBlank @RequestParam @ApiParam(value = "序号", name = "ids", required = true) String ids) {
		int result = twbDataDicService.deleteTwbDataDicByIds(ids);
		if (result > 0) {
			return Results.success("删除成功");
		} else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title  logicDeleteTwbDataDicById
	 * @Description 逻辑删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2020-07-27 15:33:39
	 */
	/*
	 * @PostMapping("/logicDeleteById")
	 * 
	 * @ApiOperation(value = "逻辑删除信息") public Result<String>
	 * logicDeleteTwbDataDicById(
	 * 
	 * @NotBlank
	 * 
	 * @RequestParam String id){ TwbDataDic record = twbDataDicService.getById(id);
	 * if(null == record) { return Result.fail("信息不存在"); } record = new
	 * TwbDataDic(); record.setId(id); twbDataDicService.setUpdateInfo(record);
	 * record.setIsdel(1); int result = twbDataDicService.updateSelective(record);
	 * if(result>0) { return Result.success("删除成功"); }else { return
	 * Result.fail("删除失败"); } }
	 */
	/**
	 * @Title  logicBatchDeleteTwbDataDicByIds
	 * @Description 批量逻辑删除
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2020-07-27 15:33:39
	 */

	/*
	 * @PostMapping("/logicBatchDeleteByIds")
	 * 
	 * @ApiOperation(value = "逻辑删除信息") public Result<String>
	 * logicBatchDeleteTwbDataDicByIds(
	 * 
	 * @NotBlank
	 * 
	 * @RequestParam String ids){ int result =
	 * twbDataDicService.logicBatchDeleteTwbDataDicByIds(ids); if(result>0) { return
	 * Result.success("删除成功"); }else { return Result.fail("删除失败"); } }
	 */
	/**
	 * @Title  exportExcel
	 * @Description 导出excel
	 * @param exportExcelVo
	 * @param searchVo
	 * @param request
	 * @return
	 * @throws Exception 
	 * @author DSY
	 * @date 2020-07-27 15:33:39
	 */
	@Auth(verifyURL = false)
	@GetMapping("/exportExcel")
	@ApiOperation(value = "导出excel")
	public void exportExcel(SearchTwbDataDicVo vo, HttpServletResponse response) throws Exception {

		twbDataDicService.exportExcel(vo, response);
	}

	@Auth(verifyURL = false)
	@GetMapping("/listDataType")
	@ApiOperation(value = "数据字典类别查询")
	public Result<List<ListTwbDataTypeDto>> listDataType(@RequestParam(required = false) @ApiParam(value = "类别编码", name = "typeCode", required = false) String typeCode, @RequestParam(required = false) @ApiParam(value = "类别名称", name = "typeName", required = false) String typeName, @RequestParam(required = false) @ApiParam(value = "父类别Id", name = "parentId", required = false) String parentId, @RequestParam(required = false) @ApiParam(value = "查询关键字（支持编码、名称模糊查询）", name = "queryWord", required = false) String queryWord) {
		List<ListTwbDataTypeDto> list = twbDataDicService.listTwbDataType(typeCode, typeName, queryWord, parentId);
		return Results.successWithData(list);
	}

	/**
	 * @Title listDataDicByType
	 * @Description 根据类别编码查询字典信息
	 * @param typeCode
	 * @return
	 * @author DSY
	 * @date 2020年8月7日 上午10:23:48
	 */
	@Auth(verifyURL = false)
	@GetMapping("/listDataDicByType")
	@ApiOperation(value = "根据类别编码查询字典信息")
	public Result<List<TwbDataDicDto>> listDataDicByType(@RequestParam(required = false) @ApiParam(value = "类别编码", name = "typeCode", required = false) String typeCode) {
		TwbDataDic record = new TwbDataDic();
		record.setTypeCode(typeCode);
		List<TwbDataDicDto> list = new ArrayList<TwbDataDicDto>();
		List<TwbDataDic> datas = twbDataDicService.list(record);
		for (TwbDataDic twbDataDic : datas) {
			TwbDataDicDto dto = new TwbDataDicDto();
			BeanUtils.copyNotNullProperties(twbDataDic, dto);
			list.add(dto);
		}
		return Results.successWithData(list);
	}

	/**
	 * @Title listDataDicByType
	 * @Description 根据类别编码查询一个对象
	 * @param typeCode
	 * @return
	 * @author DSY
	 * @date 2020年8月7日 上午10:23:48
	 */
	@Auth(verifyURL = false)
	@GetMapping("/getDataDicByTypeCode")
	@ApiOperation(value = "根据类别编码查询一个对象")
	public Result<GetDataDicByTypeCodeDto> getDataDicByTypeCode(@RequestParam(required = false) @NotBlank @ApiParam(value = "类别编码", name = "typeCode", required = true) String typeCode) {

		TwbDataDic record = new TwbDataDic();
		record.setTypeCode(typeCode);

		List<TwbDataDic> datas = twbDataDicService.list(record);

		GetDataDicByTypeCodeDto dicByTypeCodeDto = new GetDataDicByTypeCodeDto();

		if (CollectionUtils.isNotEmpty(datas)) {
			TwbDataDic twbDataDic = datas.get(0);
			BeanUtils.copyNotNullProperties(twbDataDic, dicByTypeCodeDto);
		}

		return Results.successWithData(dicByTypeCodeDto);
	}

	/**
	 * @Title getNextSeqNumByTypeCode
	 * @Description 获取下一个顺序号
	 * @param typeCode
	 * @return
	 * @author DSY
	 * @date 2020年8月26日 下午12:45:27
	 */
	@Auth(verifyURL = false)
	@GetMapping("/getNextSeqNumByTypeCode")
	@ApiOperation(value = "获取下一个顺序号")
	public Result<Integer> getNextSeqNumByTypeCode(
			@ApiParam(value = "类别编码", name = "typeCode", required = true) String typeCode,
			@ApiParam(value = "是否使用父级ID查询", name = "isParent", required = true) String isParent
	) {
		Integer seqNum = twbDataDicService.getMaxSeqNumByTypeCode(typeCode, isParent);
		if (seqNum == null) {
			seqNum = 1;
		} else {
			seqNum = seqNum + 1;
		}
		return Results.successWithData(seqNum);
	}

	/**
	 * @Title searchTwbDataDicTreeDto
	 * @Description 字典树查询
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2020年11月5日 下午3:07:43
	 */
	@Auth(verifyURL = false)
	@GetMapping("/searchTree")
	@ApiOperation(value = "字典树查询")
	public Result<List<SearchTwbDataDicDto>> searchTwbDataDicTreeDto(SearchTwbDataDicVo vo) {
		List<SearchTwbDataDicDto> list = twbDataDicService.searchTwbDataDicTree(vo);
		return Results.successWithData(list);
	}

	/**
	 * @Title updateParentIdByTypeCode
	 * @Description 更新类别层级信息
	 * @param typeCode
	 * @param parentId
	 * @return
	 * @author DSY
	 * @date 2020年11月16日 上午10:36:57
	 */
	@Auth(verifyURL = false)
	@PostMapping("/updateParentIdByTypeCode")
	@ApiOperation(value = "更新类别层级信息")
	public Result<String> updateParentIdByTypeCode(@RequestParam(required = true) @ApiParam(value = "类别编码", name = "typeCode", required = true) String typeCode, @RequestParam(required = true) @ApiParam(value = "父类别编码", name = "parentId", required = true) String parentId) {
		int result = twbDataDicService.updateParentIdByTypeCode(typeCode, parentId);
		if (result > 0) {
			return Results.success("更新成功");
		} else {
			return Results.failure("更新失败");
		}
	}
}
